home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / tools / czesc_2 / ispell-3.3ljr / ispell / lookup.c < prev    next >
C/C++ Source or Header  |  1992-09-22  |  4KB  |  162 lines

  1. /*
  2.  * lookup.c - see if a word appears in the dictionary
  3.  *
  4.  * Pace Willisson, 1983
  5.  */
  6.  
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <fcntl.h>
  10. #include "config.h"
  11. #include "ispell.h"
  12.  
  13. struct dent *hashtbl;
  14. int hashsize;
  15.  
  16. extern char hashname[];
  17.  
  18. static inited = 0;
  19.  
  20. int linit (void)
  21. {
  22.   int hashfd;
  23.   register int i;
  24.   register struct dent *dp;
  25.  
  26.   if (inited)
  27.     return 1;
  28.  
  29.   if ((hashfd = open (hashname, 0)) < 0)
  30.     {
  31.       fprintf (stderr, "can't open %s\n", hashname);
  32.       return (-1);
  33.     }
  34.  
  35.   hashsize = read (hashfd, &hashheader, sizeof hashheader);
  36.   if (hashsize == 0)
  37.     {
  38.       /*
  39.        * Empty file - create an empty dummy table.  We
  40.        * actually have to have one entry since the hash
  41.        * algorithm involves a divide by the table size
  42.        * (actually modulo, but zero is still unacceptable).
  43.        * So we create an entry with a word of all lowercase,
  44.        * which can't match because the comparison string has
  45.        * been converted to uppercase by then.
  46.        */
  47.       close (hashfd);
  48.       hashsize = 1;        /* This prevents divides by zero */
  49.       hashtbl = (struct dent *) calloc (1, sizeof (struct dent));
  50.       if (hashtbl == NULL)
  51.     {
  52.       (void) fprintf (stderr,
  53.               "Couldn't allocate space for hash table\n");
  54.       return (-1);
  55.     }
  56.       hashtbl[0].word = "xxxxxxxxxxx";
  57.       hashtbl[0].next = NULL;
  58.       hashtbl[0].keep = 0;
  59.       hashtbl[0].used = 1;
  60.       /* The flag bits don't matter, but calloc cleared them. */
  61.       inited = 1;
  62.       return 0;
  63.     }
  64.   else if (hashsize < 0 || hashheader.magic != MAGIC)
  65.     {
  66.       fprintf (stderr, "Illegal format hash table\n");
  67.       return (-1);
  68.     }
  69.   hashstrings = (char *) malloc (hashheader.stringsize);
  70.   hashtbl = (struct dent *) malloc (hashheader.tblsize * sizeof (struct dent));
  71.   if (hashtbl == NULL || hashstrings == NULL)
  72.     {
  73.       (void) fprintf (stderr,
  74.               "Couldn't allocate space for hash table\n");
  75.       return (-1);
  76.     }
  77.   hashsize = hashheader.tblsize;
  78.  
  79.   read (hashfd, hashstrings, hashheader.stringsize);
  80.   read (hashfd, hashtbl, hashheader.tblsize * sizeof (struct dent));
  81.   close (hashfd);
  82.  
  83.   for (i = hashsize, dp = hashtbl; --i >= 0; dp++)
  84.     {
  85.       dp->word = &hashstrings[(int) (dp->word)];
  86.       if (dp->next == (struct dent *) - 1)
  87.     dp->next = NULL;
  88.       else
  89.     dp->next = &hashtbl[(int) (dp->next)];
  90.     }
  91.  
  92.   inited = 1;
  93.   return (0);
  94. }
  95.  
  96. /* n is length of s */
  97. struct dent *lookup (char *s, int n, int dotree)
  98. {
  99.   int i;
  100.   register struct dent *dp;
  101.   register char *s1, *s2;
  102.  
  103.   dp = &hashtbl[hash (s, n, hashsize)];
  104.   for (; dp != NULL; dp = dp->next)
  105.     {
  106.       /* quick strcmp, but only for equality */
  107.       s1 = dp->word;
  108.       s2 = s;
  109.       while (*s1 == *s2++)
  110.     if (*s1++ == '\0')
  111.       {
  112.         lastdent = dp;
  113.         return (lastdent);
  114.       }
  115.     }
  116.   if (dotree)
  117.     {
  118.       i = s[n];
  119.       s[n] = '\0';
  120.       if ((dp = treelookup (s)) != NULL)
  121.     lastdent = dp;
  122.       s[n] = i;
  123.       return dp;
  124.     }
  125.   else
  126.     return NULL;
  127. }
  128.  
  129. #include <ctype.h>
  130. /* check the hashed dictionary for words matching the regex. return the */
  131. /* a matching string if found else return NULL */
  132. char *do_regex_lookup (char *regex, int whence)
  133.  /* regular expression to use in the match   */
  134.  /* 0 = start at the beg with new regx, else */
  135.  /* continue from cur point w/ old regex     */
  136. {
  137.   static curindex = 0;
  138.   static char *cmp_expr;
  139.   char *re_comp ();
  140.  
  141.   if (whence == 0)
  142.     {
  143.       char *ptr = regex;
  144.       while (*ptr)
  145.     {
  146.       if (islower (*ptr))
  147.         *ptr = toupper (*ptr);
  148.       ptr++;
  149.     }
  150.       cmp_expr = re_comp (regex);
  151.       curindex = 0;
  152.     }
  153.  
  154.   /* search the dictionary until the word is found or the words run out */
  155.   for (; curindex < hashsize; curindex++)
  156.     {
  157.       if (re_exec (hashtbl[curindex].word))
  158.     return (hashtbl[curindex++].word);
  159.     }
  160.   return (NULL);
  161. }
  162.